from django.apps import apps
from django.conf import settings

from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.sites.models import Site
from django.utils.translation import gettext_lazy as _


from allauth.socialaccount.models import SocialAccount, SocialApp, SocialToken
from allauth.account.models import EmailAddress
from rest_framework_simplejwt.token_blacklist.models import BlacklistedToken
from rest_framework.authtoken.models import TokenProxy

# from django.contrib.auth.models import Group

from tsl_backend.users.forms import UserAdminChangeForm, UserAdminCreationForm
import tsl_backend.users.models as users


class UserAdmin(auth_admin.UserAdmin):
    form = UserAdminChangeForm
    add_form = UserAdminCreationForm
    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (_("Personal info"), {"fields": ("name", "email", "cnName", "staffcode")}),
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                ),
            },
        ),
        (_("Important dates"), {"fields": ("last_login", "date_joined")}),
    )
    list_display = ["username", "name", "is_superuser"]
    search_fields = ["name"]


admin.site.register(users.User, UserAdmin)


class DepartmentAdmin(admin.ModelAdmin):
    list_display = ["code", "name"]
    search_fields = ["code", "name"]


admin.site.register(users.Department, DepartmentAdmin)


class RolePermissionAdmin(admin.ModelAdmin):
    
    list_filter = ["role", "flow"]
    list_display = ["role", "flow", "show_Order_Number", "show_Due_Date", "show_Quantity",
                    "show_Cargo_type", "show_Description", "show_Product_Size", "show_Fineness", "show_Purchases_Unit",
                    "show_Stone_Date", "show_Flower_Patten", "show_Manifest_Type", "show_Design_Number", "show_Model_Number",
                    "show_Black_And_White_Photo", "show_Production_Instructions", "show_Result", "show_Company_Engraving_Content",
                    "show_Company_Engraving_Font", "show_Customer_Engraving_Content", "show_Customer_Engraving_Font", "show_Engraving_Description",
                    "show_Engraving_Effect", "show_Ingredients", "show_Stone_Issue", "show_Stone_Detail", "show_Production_Notes", "show_Process",
                    "show_Difficulty", "show_Divided_member", "show_Production_weight", "show_Registration_From", "show_Row_Stone_Image", "show_Colour_Image",
                    "menu_massscan_perm", "menu_orderview_perm", "menu_printlabel_perm",
                    "report_worker_storage_perm", "report_worker_transaction_perm", "report_worker_handover_perm",
                    "report_worker_handset_perm", "report_qc_handset_perm", "report_qc_transaction_perm",
                    "report_printstatus_perm", "report_leadtime_perm", "menu_updatert_perm"]
    search_fields = ["role__chinese_name", "flow__chinese_name"]
    fieldsets = [
        (
            "工单资料",
            {
                "classes": ["collapse", "open"],
                "fields": ["role", "flow", "show_Order_Number", "show_Due_Date", "show_Quantity",
                    "show_Cargo_type", "show_Description", "show_Product_Size", "show_Fineness", "show_Purchases_Unit",
                    "show_Stone_Date", "show_Flower_Patten", "show_Manifest_Type", "show_Design_Number", "show_Model_Number",
                    "show_Black_And_White_Photo", "show_Production_Instructions", "show_Result", "show_Company_Engraving_Content",
                    "show_Company_Engraving_Font", "show_Customer_Engraving_Content", "show_Customer_Engraving_Font", "show_Engraving_Description",
                    "show_Engraving_Effect", "show_Ingredients", "show_Stone_Issue", "show_Stone_Detail", "show_Production_Notes", "show_Process",
                    "show_Difficulty", "show_Divided_member", "show_Production_weight", "show_Registration_From", "show_Row_Stone_Image", "show_Colour_Image",
                    ],
            },
        ),
        (
            "权限",
            {
                "classes": ["collapse", "open"],
                "fields": ["menu_massscan_perm", "menu_orderview_perm", "menu_printlabel_perm",
                    "report_worker_storage_perm", "report_worker_transaction_perm", "report_worker_handover_perm",
                    "report_worker_handset_perm", "report_qc_handset_perm", "report_qc_transaction_perm",
                    "report_printstatus_perm", "report_leadtime_perm", "menu_updatert_perm"],
            },
        ),
    ]


admin.site.register(users.RolePermission, RolePermissionAdmin)


class PrinterAdmin(admin.ModelAdmin):
    list_display = ["description", "get_department", "group", "get_site", "printId", "default"]
    list_filter = ["group"]

    def get_department(self, obj):
        return obj.get_department_display()
    
    def get_site(self, obj):
        return obj.get_site_display()

    get_department.short_description="仓"
    get_site.short_description="內/外销"


admin.site.register(users.Printer, PrinterAdmin)


class RoleAdmin(admin.ModelAdmin):
    list_display = ["name", "chinese_name"]
    search_fields = ["name", "chinese_name"]


admin.site.register(users.Role, RoleAdmin)


class FlowAdmin(admin.ModelAdmin):
    list_display = [
        "name",
        "chinese_name",
        "scan_and_record",
        "qc_check",
        "leader_check",
        "stone_exam",
        "dispatcher_check",
    ]
    search_fields = ["chinese_name", "name"]


admin.site.register(users.Flow, FlowAdmin)


class SiteAdmin(admin.ModelAdmin):
    list_display = ["name"]
    search_fields = ["name"]


admin.site.register(users.Site, SiteAdmin)


# if settings.DEBUG:

@admin.register(users.Account)
class AccountAdmin(admin.ModelAdmin):
    list_display = ["django_user", "id", "user_code", "role", "department"]
    search_fields = ["django_user", "id"]


# class NameOnly(admin.ModelAdmin):
#     list_display = ["name"]
#     search_fields = ["name"]


# for model in apps.get_app_config("users").get_models():
#     try:
#         admin.site.register(model, NameOnly)
#     except admin.sites.AlreadyRegistered:
#         pass


admin.site.unregister(SocialAccount)
admin.site.unregister(SocialApp)
admin.site.unregister(SocialToken)
admin.site.unregister(EmailAddress)
admin.site.unregister(BlacklistedToken)
admin.site.unregister(Site)
admin.site.unregister(TokenProxy)
